<?php

declare(strict_types=1);

namespace app\wechat\controller\admin;

use think\Request;
use think\Response;
use think\facade\Db;
use sheep\controller\Backend;
use app\wechat\facade\Wechat;
use app\wechat\model\Reply as ReplyModel;
use sheep\exception\SheepException;

class Reply extends Backend
{

    protected $wechat = null;
    protected $model = null;

    public function initialize()
    {
        $this->wechat = Wechat::officialAccount();
        $this->model = new ReplyModel;
    }
    /**
     * 菜单列表
     *
     * @return Response
     */
    public function list(): Response
    {
        $group = $this->request->param('group', 'keywords');
        $data = $this->model->sheepFilter()->where('group', $group)->select(); 
        return success('操作成功', $data);
    }

    /**
     * 菜单详情
     *
     * @param  int $id
     * @return \think\Response
     */
    public function detail($id)
    {
        $data = $this->model->findOrFail($id);
        return success('获取成功', $data);
    }


    /**
     * 添加菜单
     *
     * @param  int $publish   发布状态:0=不发布,1=直接发布
     * @return \think\Response
     */
    public function add()
    {
        $params = $this->request->only(['group', 'keywords', 'type', 'status', 'content']);

        // 参数校验
        // $this->svalidate($params, '.add');

        $result = Db::transaction(function () use ($params) {
            $result = $this->model->save($params);
            if($result) {
                $reply = $this->model;
                if($reply->group !== 'keywords' && $reply->status === 'enable') {
                    $this->model->where('group', $reply->group)->where('id', '<>', $reply->id)->enable()->update(['status' => 'disabled']);
                }
            }
            return $result;
        });
        
        if($result) {
            return success('保存成功');
        }else {
            return error('保存失败');
        }
    }

    /**
     * 编辑
     *
     * @param  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        $reply = $this->model->findOrFail($id);
        $params = $this->request->only(['keywords', 'type', 'status', 'content']);

        // 参数校验
        // $this->svalidate($params);

        $result = Db::transaction(function () use ($params, $reply) {
            $result = $reply->save($params);
            if($result) {
                if($reply->group !== 'keywords' && $reply->status === 'enable') {
                    $this->model->where('group', $reply->group)->where('id', '<>', $reply->id)->enable()->update(['status' => 'disabled']);
                }
            }
            return $result;
        });

        if($result) {
            return success('更新成功', $result);
        }else {
            return error('更新失败');
        }
    }

    /**
     * 删除
     *
     * @param  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $is_real = $this->request->param('is_real', 0);
        Db::transaction(function () use ($id, $is_real) {
            $reply = $this->model->findOrFail($id);
            if ($is_real) {
                $reply->force()->delete();
            } else {
                $reply->delete();
            }
        });

        return success('删除成功');
    }
}
